import { _decorator, color, Color, Component, Label, Node, Sprite } from 'cc';
import { TreaIng } from '../entity/TreaIng';
import TreaApi from '../api/TreaApi';
import { trea } from './Trea';
import { EventManager } from '../event/EventManager';
import { nftdata, PurchaseConfig, PurchaseType } from "../settings/const";
const { ccclass, property } = _decorator;

@ccclass('Inventory')
export class Inventory extends Component {
    /**宝物图片地址 */
    @property(Sprite)
    imgUrl: Sprite
    /**宝物名称 */
    @property(Label)
    nameLabel: Label
    /**宝物加成 */
    @property(Label)
    buff: Label

    /**销毁按钮样式 */
    @property(Sprite)
    private destroySprite: Sprite
    /**洗练按钮样式 */
    @property(Sprite)
    private refineSprite: Sprite
    /**铸造NFT按钮样式 */
    @property(Sprite)
    private castNftSprite: Sprite

    /**销毁按钮字体样式 */
    @property(Label)
    destroyLable: Label
    /**洗练按钮字体样式 */
    @property(Label)
    refineLable: Label
    /**铸造NFT按钮字体样式 */
    @property(Label)
    castNftLable: Label
    /**成功失败弹窗 */
    @property(Node)
    over: Node
    /**成功失败弹窗内容 */
    @property(Label)
    yes: Label

    /**销毁 */
    @property(Node)
    destroyBtn: Node
    /**洗练 */
    @property(Node)
    refine: Node
    /**铸造NFT */
    @property(Node)
    castNft: Node
    trea: TreaIng;

    start() {
        this.over.active = false
        /**销毁 */
        this.destroyBtn.on(Node.EventType.TOUCH_END, this.destroyBtnFun, this)
        /**洗练 */
        this.refine.on(Node.EventType.TOUCH_END, this.refineBtn, this)
        /**铸造Nft */
        this.castNft.on(Node.EventType.TOUCH_END, this.castNftBtn, this)
    }


    /**销毁 */
    async destroyBtnFun() {
        console.log("销毁")
        const isTurnApi = await TreaApi.totemFounding(27, "founding", this.trea.index, 4)
        if (isTurnApi.code == 0) {
            // 调用父组件getList方法
            EventManager.instance.emit(EventManager.EventType.RefreshTreaList, 1)
        }
    }
    /**洗练 */
    async refineBtn() {
        if (this.trea.grade > 1) {
            console.log("洗练")
            const isTurnApi = await TreaApi.totemFounding(27, "founding", this.trea.index, 5)
            if (isTurnApi.code == 0) {
                this.over.active = true
                this.yes.string = "洗练成功"
                setTimeout(() => {
                    EventManager.instance.emit(EventManager.EventType.RefreshTreaList, 1)
                }, 1000)
                // setTimeout(() => {

                // }, 1000)
                // 调用父组件getList方法
            }
        }
    }
    /**铸造NFT */
    async castNftBtn() {
        if (nftdata[this.trea.id] && nftdata[this.trea.id] != undefined) {
            console.log("铸造NFT")
            const isTurnApi = await TreaApi.totemFounding(27, "founding", this.trea.index, 6)
            if (isTurnApi.code == 0) {
                // 调用父组件getList方法
                EventManager.instance.emit(EventManager.EventType.RefreshTreaList, 1)
            }
        }
    }

    async setData(trea: TreaIng) {
        
        /**当前宝库数据 */
        this.trea = trea
        this.imgUrl.spriteFrame = await trea.getTreaIngImg()
        this.refineLable.string = '洗练(' + PurchaseConfig[PurchaseType.Founding].price + '元宝)'
        this.castNftLable.string = '铸造NFT(' + PurchaseConfig[PurchaseType.Mint].price + '元宝)'

        // 判断是否可洗练,并隐藏洗练按钮  1:不可洗练  4:可洗练
        if (this.trea.grade == 1) {
            this.refineLable.string = '不可洗练'
         
        }
        // 判断是否铸造NFT,并隐藏铸造NFT按钮  nftdata内包含当前宝库数据就可以铸造   反之不可铸造
        if (nftdata[this.trea.id] == undefined || !nftdata[this.trea.id]) {
            this.castNftLable.string = '不可铸造NFT'
            
        }
        this.nameLabel.string = this.trea.name
        if (this.trea.id % 10 == 0) {
            this.nameLabel.color = new Color(255, 255, 255)
        } else if (this.trea.id % 10 == 1) {
            this.nameLabel.color = new Color(149, 242, 4)
        } else if (this.trea.id % 10 == 2) {
            this.nameLabel.color = new Color(0, 255, 255)
        } else if (this.trea.id % 10 == 3) {
            this.nameLabel.color = new Color(255, 255, 0)
        }
        this.buff.string = this.trea.effectType.name + '增加' + this.trea.effectType.addion * 100 + '%'
    }
}


